package org.example.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.example.admin.common.biz.user.UserContext;
import org.example.admin.common.convention.result.Result;
import org.example.admin.common.convention.result.Results;
import org.example.admin.dao.entity.GroupDO;
import org.example.admin.remote.dto.req.ShortLinkRecycleBinPageReqDTO;
import org.example.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.example.admin.service.GroupService;
import org.example.admin.service.RecycleBinShortLinkRemoteService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author weibin
 * @since 2025/10/14 21:35
 */
@Service
@RequiredArgsConstructor
public class RecycleBinShortLinkRemoteServiceImpl implements RecycleBinShortLinkRemoteService {

    private final GroupService groupService;

    @Override
    public Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        // 获取当前用户的分组
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getDelFlag, 0);
        List<GroupDO> groupDOs = groupService.list(queryWrapper);
        if (CollUtil.isEmpty(groupDOs)) {
            return Results.success(new Page<>());
        }
        requestParam.setGidList(groupDOs.stream().map(GroupDO::getGid).toList());
        String responseStr = HttpUtil.post("127.0.0.1:8002/recycle-bin/pageShortLink", JSON.toJSONString(requestParam));
        return JSON.parseObject(responseStr, new TypeReference<>() {});
    }

}
